/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author luan
 */
import java.util.*;
import java.io.*;
public class Concordance 
{
    private TreeMap ccDance= new TreeMap();
    private String fSource;// the source file to process
    private String fTarget;// the result file after process
    public Concordance(String source, String target)
    {
        fSource=source;
        fTarget=target;
    }
    public TreeMap createConcordance() throws Exception
    {        
        //FileReader rd = new FileReader(fSource);
        //FileWriter wt = new  FileWriter(fTarget);
        // The below statements are used for UTF8 encoding 
        
        InputStreamReader rd = new InputStreamReader(new FileInputStream(fSource), "UTF-8");
        //create a stream to read from file can read UTF-8(unicode)
        OutputStreamWriter wt = new OutputStreamWriter(new FileOutputStream(fTarget),"UTF-8");
        //create a stream to write to file
        OutputStreamWriter ow = new OutputStreamWriter(System.out,"UTF-8");
        //Create a stream to display to screen
        //PrintWriter pw = new PrintWriter(System.out,true);
        String word=""; // store a single word after read from file
        int c;// store single character after read from file
        int lineNum=1;// store the line of word appears in the file
        
        while((c=rd.read())!=-1)
        {
            //check the character read from file:
            //if is character or digit follow a charater(not the digit stand alone)
            if (Character.isLetter(c) || (Character.isDigit(c)&& word.compareTo("")!=0))
            {
                
                word=word + (char)c; //add c to word
            }
            else //if not that means meet the space not a charactor
            {   //check if the length of word is > 3 or not "the"
               if(word.length()>=3 && !word.equalsIgnoreCase("the"))
               {   //create a arraylist and assgin the value of objectkey in treemap ccDance
                   ArrayList setLines=(ArrayList)ccDance.get(word.toLowerCase());
                   //if setLine not null(means this word exist in ccDance)
                   if (setLines!=null )
                        //if lineNum does not exist in setLine
                        {if (!setLines.contains(lineNum))
                           setLines.add(lineNum);// add the lineNum to setLine
                        }
                     else // the word does not exist in ccDance
                    {
                         ArrayList obj= new  ArrayList();  //create new ArrayList Obj
                         obj.add(lineNum);// add lineNum into obj
                         ccDance.put(word.toLowerCase(), obj);//put the new word and line to ccDance
                    }
               }
                   word=""; //after finished reading one word, reset word
                   if (c==13) // c==13 means "enter"
                             lineNum++;// add one more line
            }
            
        }
        
        Set setkey= ccDance.keySet(); //create a new Set setkey and assign  all the key of ccDace to setkey
        ArrayList lines; //create a new ArrayList lines
        String st;
        for(Iterator i=setkey.iterator();i.hasNext();)
        {  //loop until end of setkey
            st="";
            word=(String)i.next();//assing the word in the list to word
            lines=(ArrayList)ccDance.get(word); //assign the line of word appear to array list lines
            wt.write(word); //write word into file
            ow.write(word);//write word to screen
            for(Object j:lines) // loop to write lines that word appeared into file
                st= st + " " +((Integer) j).toString();
            wt.write(" " + st + "\n");
            ow.write(" " + st + "\n");
        }
        rd.close();
        wt.close();
        return ccDance;
        
         
    }
    
}
